package com.newbee.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.adapter.InviteMultCharAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.fragement.multchat.MultChatFragement;
import com.newbee.listbean.InviteMultBean;
import com.newbee.multchat.MultChatActivity;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceKey;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.sqlite.DBHelper;

public class RequestMultChatListActivity extends Activity implements
		OnItemClickListener, OnRefrushActivity, OnClickListener
{

	private ListView mListView;

	private InviteMultCharAdapter mAdapter;

	private List<InviteMultBean> mListDatas;

	private DBHelper mDbHelper;

	private AlertDialog.Builder multchatBuilder;
	private String roomName;
	private int selectID;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_luyin_dongtai_list);
		this.findViewById(R.id.mult_requ_list_back).setOnClickListener(this);
		mDbHelper = new DBHelper(this);
		initAlertDialog();
		initLayout();
		initListView();
	}

	private void initAlertDialog()
	{
		multchatBuilder = new Builder(this);
		multchatBuilder.setTitle("协同录音请求");
		multchatBuilder.setMessage("是否接受协同录音请求？");

	}

	private void initListView()
	{
		mListDatas = mDbHelper.getAllInviteMultChat();
		if (mAdapter == null)
		{
			mAdapter = new InviteMultCharAdapter(mListDatas, this);
			mListView.setAdapter(mAdapter);
		}
		else
		{
			mAdapter.setList(mListDatas);
			mAdapter.notifyDataSetChanged();
		}

	}

	private void initLayout()
	{
		this.mListView = (ListView) this
				.findViewById(R.id.listview_invite_mult_chat_O);
		this.mListView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3)
	{

		TextView status = (TextView) v.findViewById(R.id.invite_list_state);

		InviteMultBean bean = mListDatas.get(p);
		roomName = bean.getRoomName();
		selectID = bean.getIid();
		if (status.getText().toString().equals("[未处理]"))
		{
			multchatBuilder.setPositiveButton("接受",
					new DialogInterface.OnClickListener()
					{

						@Override
						public void onClick(DialogInterface arg0, int arg1)
						{

							SmackClient sc = SmackClient.getInstance();

							Map<String, Object> paras = new HashMap<String, Object>();
							paras.put(MainServiceKey.MULT_CHAT_ROOM_NAME,
									roomName.split("@")[0]);

							MainServiceTask msk = new MainServiceTask(
									TaskType.ACCEPT_MULT, paras,
									RequestMultChatListActivity.class.getName());

							MainService.addActivityMap(
									RequestMultChatListActivity.class.getName(),
									RequestMultChatListActivity.this);
							MainService.addTaskQueue(msk);

						}
					});

			multchatBuilder.setNegativeButton("拒绝",
					new DialogInterface.OnClickListener()
					{

						@Override
						public void onClick(DialogInterface arg0, int arg1)
						{

							mDbHelper.solveInviteMultChat(selectID);

						}
					});
			multchatBuilder.create().show();
		}

	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{

		switch (taskID)
		{
		case TaskType.ACCEPT_MULT:
		{
			mDbHelper.solveInviteMultChat(selectID);
			Intent intent = new Intent(RequestMultChatListActivity.this,
					MultChatActivity.class);

			String[] array = roomName.split("@")[0].split("\\$");
			String pureName = array[array.length - 1];
			intent.putExtra(MultChatFragement.CHAT_ROOM_MAME, pureName);// 纯md5
			intent.putExtra(MultChatFragement.INIT_STATE, 2);
			intent.putExtra(MultChatFragement.MASTER_TAG, false);
			RequestMultChatListActivity.this.startActivity(intent);
			break;
		}

		default:
			break;
		}
		return null;

	}

	@Override
	protected void onDestroy()
	{

		MainService.removeActivity(RequestMultChatListActivity.class.getName());
		super.onDestroy();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.mult_requ_list_back:
			this.finish();

			break;

		default:
			break;
		}

	}
}
